package com.jiulong.springboot_validator.controller;

import com.jiulong.springboot_validator.annotation.NotResponseResult;
import com.jiulong.springboot_validator.model.domain.Order;
import com.jiulong.springboot_validator.model.domain.Person;
import com.jiulong.springboot_validator.model.result.common.Result;
import com.jiulong.springboot_validator.model.result.common.ResultCode;
import com.jiulong.springboot_validator.service.ValidatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@Api(tags = "参数校验测试接口")
@RestController
@Validated//配合@Valid完成嵌套验证
//海云真美
@RequestMapping("/validate")
public class ValidatorController {
    @Autowired
    private ValidatorService validatorService;

    @ApiOperation(value = "校验String数据类型")
    @GetMapping("/str")
    @NotResponseResult //若加上此注解,则返回结果不进行Result包装
    public String str(@NotBlank(message = "字符串不能为空") @RequestParam("str") String str) {
        return str;
    }

    @ApiOperation(value = "校验对象")
    @PostMapping("/order")
    public Order order(@Valid @RequestBody Order order) {
        return order;
    }

    @ApiOperation(value = "校验嵌套对象",notes ="若返回的对象为Result,则框架不处理;若返回对象不是Result,也没有加@NotResponseResult注解,则框架自动用Result包装后再返回" )
    @PostMapping("/person")
    public Result<Person> person(@Valid @RequestBody Person person) {
        return new Result<>(ResultCode.SUCCESS,person);
    }

    @ApiOperation(value = "校验自定义异常全局统一处理")
    @GetMapping("/getOrder")
    public Order getOrder() {
        return validatorService.getOrder();
    }

}
